package com.anjho.pojo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "sale")
@NamedQueries(value = {
		@NamedQuery(name = "Sale.getSaleList", query = "SELECT s FROM Sale s"),
		@NamedQuery(name = "Sale.getSaleBySalecode", query = "SELECT s FROM Sale s WHERE s.salecode = :salecode")
})
public class Sale implements Serializable {

	private static final long serialVersionUID = 913432155203391627L;
	
	public Sale() {
		this.setSaleDate(new Date());
	}
	
	public Sale(Client client){
		this.client = client;
		this.setSaleDate(new Date());
	}

	@Id
	@Column(name="salecode")
	@OneToMany(mappedBy="sale")
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private Long salecode;
	
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "saleDate", nullable = false)
	private Date saleDate;
	
	@Column(name = "value", nullable = false, precision = 10, scale=2)
	private BigDecimal value;
	
	@ManyToOne
	@JoinColumn(name="user_login", nullable=false)
	private User user;
	
	@ManyToOne
	@JoinColumn(name="client", nullable=false)
	private Client client;
	
	@ManyToMany
	@JoinTable(name = "sale_products", joinColumns = { @JoinColumn(name = "salecode") }, inverseJoinColumns = { @JoinColumn(name = "prcode") })
	private List<Product> productList;

	public Long getSalecode() {
		return salecode;
	}

	public void setSalecode(Long salecode) {
		this.salecode = salecode;
	}

	public Date getSaleDate() {
		return saleDate;
	}

	public void setSaleDate(Date saleDate) {
		this.saleDate = saleDate;
	}

	public BigDecimal getValue() {
		return value;
	}

	public void setValue(BigDecimal value) {
		this.value = value;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((salecode == null) ? 0 : salecode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Sale other = (Sale) obj;
		if (salecode == null) {
			if (other.salecode != null)
				return false;
		} else if (!salecode.equals(other.salecode))
			return false;
		return true;
	}

}
